<?php

namespace App\Http\Controllers\Api;

use App\Models\EmailLog;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\Mail;

class EmailController extends Controller
{
    private $mailer;

    public function __construct(Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function send(Request $request)
    {
        $email = $request->post('email');
        if(!$email){
            return returnError("邮箱不能为空");
        }
        $title = $request->post('title');
        $content = $request->post('content');
        $date = date("Y-m-d H:i:s");

        //$email = '1058424094@qq.com';
        //$title = '测试邮件001';
        //$content = '测试邮件001';//内容

        $tag = $this->mailer->raw($content, function ($message)use($title,$email){
            $message->subject($title);
            $message->to($email);
        });

        EmailLog::create([
            'email_to' => $email,
            'title' => $title,
            'content' => $content,
            'email_from' => env("MAIL_FROM_ADDRESS"),
            'created_at' => $date,
            'updated_at' => $date,
        ]);

        return returnSuccess("发送成功");
    }


//    public function sendHtml(){
//        $data = ['title' => '你若盛开，清风自来','author' => '木心'];
//        $tag = $this->mailer->send('mail',$data,function ($message){
//            $message->subject('Hello My Dear,let`s go');
//            $message->to('244250933@qq.com');
//        });
//        return $tag;
//    }

//}

}
